layui.use(['layer', 'form', 'table', 'admin', 'ax', 'func', 'upload', 'laydate'], function () {
    var $ = layui.$;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var upload = layui.upload;
    var laydate = layui.laydate;

    /**
     * 系统管理--消息管理
     */
    var Notice = {
        tableId: "noticeTable"    //表格id
    };

    Notice.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', align: "center", hide: true, sort: false, title: 'id'},
            {field: 'name', align: "left", sort: false, title: '店名',width:220},
            {field: 'yaoma', align: "left", sort: false, title: '妖码',width:150},
            {field: 'dmSta', align: "center", sort: false, title: '状态',width:150, templet : function (d){
                if (d.dmSta == 0){

                    return "<span style=\"color: #AAAAAA\">未确认</span>";
                }else if (d.dmSta == 1){
                    return "<span style=\"color: #1E9FFF\">已确认</span>";
                }else {
                    return "";
                }
                }},
            {align: 'left', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击清空按钮
     */
    Notice.reset = function () {
        $('input').val('');
        $("#name").val('');
        $("#dmSta").val('');
        form.render();
    };


    /**
     * 点击查询按钮
     */
    Notice.search = function () {
        var queryData = {};
        queryData['name'] = $("#name").val();
        queryData['dmSta'] = $("#dmSta").val();
        table.reload(Notice.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 点击确认店名按钮
     */
    Notice.confirmExport = function () {
        var checkRows = table.checkStatus(Notice.tableId);
        var data = checkRows.data;
        var ids = "";
        if (checkRows.data.length === 0) {
            Feng.error("请选择要批量确认店名的数据");
        } else {
            for (var i in data) {
                ids += data[i].id + ",";
            }
            var operation = function () {
                var ajax = new $ax(Feng.ctxPath + "/project/syncDmBatch", function (data) {
                    if (data.code = 200) {
                        Feng.success("同步成功!");
                        table.reload(Notice.tableId);
                    } else {
                        Feng.success(data.message);
                    }

                }, function (data) {
                    Feng.error("同步失败!" + data.responseJSON.message + "!");
                });
                ajax.set("ids", ids);
                ajax.start();
            };
            Feng.confirm("是否批量确认店名?", operation);
        }
    };


    // 清空按钮点击事件
    $('#btnReset').click(function () {
        Notice.reset();
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        Notice.search();
    });

    // 确认店名按钮点击事件
    $('#confirmExport').click(function () {
        Notice.confirmExport();
    });

    /**
     * 确定
     */
    Notice.onViewNotice = function (data) {
        var operation = function () {
            var id = data.id;
            var name = data.name;
            var yaoma = data.yaoma;
            var ajax = new $ax(Feng.ctxPath + "/project/syncDm", function (data) {
                Feng.success("同步成功!");
                table.reload(Notice.tableId);
            });
            ajax.set("id", id);
            ajax.set("name", name);
            ajax.set("yaoma", yaoma);
            ajax.start();
        };
        Feng.confirm("是否确定将店名同步到门店装修 ?", operation);
    };


    /**
     * 点击修改
     *
     * @param data 点击按钮时候的行数据
     */
    Notice.onEditNotice = function (data) {
        func.open({
            height: 240,
            width:'550px',
            title: '修改',
            content: Feng.ctxPath + "/project/confirmEdit/" + data.id,
            tableId: Notice.tableId
        });
    };

    // 渲染表格
    var tableResult = table.render({
        elem: "#" + Notice.tableId,
        url: Feng.ctxPath + '/project/confirmList',
        page: true,
        cellMinWidth: 100,
        limit: 20,
        height: "full-59",
        cols: Notice.initColumn(),
        where: {status: $('#status').val()}
    });


    // 工具条点击事件
    table.on('tool(' + Notice.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        if (layEvent === 'edit') {
            Notice.onEditNotice(data);
        } else if (layEvent === 'confirm') {
            Notice.onViewNotice(data);
        }
    });
});